define(['jquery', 'bootstrap', 'backend', 'csmtable', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search:false,
                searchFormVisible:true,
                // asyndownload:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'username', title: '真实姓名', operate: 'LIKE'},
                        {field: 'beans', title: '当前吃豆', operate: 'BETWEEN', sortable: true},
                        // {field: 'bio', title: '不满意反馈次数', operate: false},
                        {field: 'gender', title: '完成订单次数', operate: false},
                        {field: 'token', title: '累计返现', operate: false},
                        // {field: 'email', title: '首次下单定位', operate: false},
                        {field: 'salt', title: '最近下单时间', operate: false},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'正常': '正常', '注销': '注销', '已限制': '已限制','已拉黑': '已拉黑'}},
                        // {field: 'verification', title: '拉黑原因', operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                        buttons: [
                        {
                            name: "xz",
                            title: "限制",
                            classname: "btn btn-xs btn-warning btn-magic btn-ajax",
                            icon: "fa",
                            text: "限制",
                            confirm: "确认限制？",
                            url: "user/user/xz",
                            visible: function(row){
                                if ("正常" == row.status) return ! 0
                            }, 
                            refresh: true
                        },
                        {
                            name: "hfxz",
                            title: "恢复限制",
                            classname: "btn btn-xs btn-success btn-magic btn-ajax",
                            icon: "fa",
                            text: "恢复限制",
                            confirm: "确认恢复限制？",
                            url: "user/user/hfxz",
                            visible: function(row){
                                if ("已限制" == row.status) return ! 0
                            }, 
                            refresh: true
                        },
                        {
                            name: "hfjy",
                            title: "恢复禁用",
                            classname: "btn btn-xs btn-success btn-magic btn-ajax",
                            icon: "fa",
                            text: "恢复禁用",
                            confirm: "确认恢复禁用？",
                            url: "user/user/hfjy",
                            visible: function(row){
                                if ("已拉黑" == row.status) return ! 0
                            }, 
                            refresh: true
                        },
                        {
                            name: "item",
                            classname: "btn btn-xs btn-success btn-dialog",
                            icon: "fa",
                            text: "操作日志",
                            url: "user/user/item",
                            dropdown: "更多"
                        },
                        {
                            name: "edit",
                            text: "详情",
                            classname: "btn btn-xs btn-info btn-dialog",
                            icon: "fa fa-eye",
                            url: "user/user/edit",
                            dropdown: "更多"
                        },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        roll: function () {
            Controller.api.bindevent();
        },

        lh: function () {
            Controller.api.bindevent();
        },
        beans: function () {
            let ids = $("input[name=user_id]").val();
            $(document).on('click', '.btn-callback', function () {
                let field = $(this).data('field');
                let val = $("input[name=" + field + "]").val();
                Fast.api.ajax({
                    url: "user/user/beans/ids/" + ids,
                    data: {field: field, val: val},
                    success: function (data, ret) {
                        console.log('ajax', data, ret)
                        // Layer.close(index);
                        if (data.code == 1) {
                            $("input[name=" + field + "]").val(0);
                            $("." + field).text(data.data);
                            Layer.closeAll();
                            Layer.alert(data.msg);
                            return false;
                        } else {
                            Layer.closeAll();
                            Layer.alert(data.msg, {title: __('Warning'), icon: 1});
                            return false;
                        }
                    }
                });
                // Fast.api.close($("input[name=callback]").val());
            })
        },
        item: function () {
            var ids = $("#assign-data-ids").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/user/item/ids/' + ids,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: '昵称', align: 'left'},
                        {field: 'user.mobile', title: '手机号', align: 'left'},
                        {field: 'user.equipment_code', title: '设备码', align: 'left'},
                        {field: 'user.joinip', title: '加入IP', align: 'left'},
                        {field: 'type', title: '拉黑方式', align: 'left'},
                        {field: 'reason', title: '拉黑原因', align: 'left'},
                        {field: 'create_dt', title: '时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'mode_text', title: '日志', align: 'left'},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});